<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Keu_setup_report_item extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		$this->load->model('keu_setup_report_item_model', 'GLReport');
		
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Setup Setup Report Item";
		$data['data'] = null;
		$data['arr_report'] = json_encode($this->GLReport->SWReportType());
		viewByTemplate('keu_setup_report_item/index', $data, 'grid');
	}
	
	function getdata()
	{
		$this->secure_session();
		echo $this->GLReport->GetJSON();
	}
	
	function dtl_report_coa()
	{
		$this->secure_session();
		echo $this->GLReport->GetDetailJSON(post('row_id'));
	}
	
	function combogrid_data(){
		$this->secure_session();
		echo json_encode($this->GLReport->GetComboGridData());
	}
		
	function print_report($as_report_code=''){
		/*$as_jurnal			= get('code_jurnal');
		$adt_date_from		= get('date_from');
		$adt_date_to		= get('date_to');
		$as_office_code		= get('office_code');
		$as_project_code	= get('project_code');
		
		if($as_jurnal!='' && $adt_date_from!='' && $adt_date_to!=''){*/
			
			// Load FPDF
			$this->load->helper('fpdf');
			
			// Set Data
			$arr_type = $this->GLReport->GetReportType($as_report_code);
			
			if ($arr_type['report_type']=='L'){
				$data['data']	   = $this->GLReport->GetReportDataLabaRugi('RPT001');
				$this->load->view('keu_setup_report_item/print_report', $data);
			}else if ($arr_type['report_type']=='N'){ 
				$data['data']	   = $this->GLReport->GetReportDataNeraca('RPT002');
				$this->load->view('keu_setup_report_item/print_report_neraca', $data);
			}else if ($arr_type['report_type']=='R'){ 
				$data['data']	   = $this->GLReport->GetReportDataNeracaLajur('RPT004');
				$this->load->view('keu_setup_report_item/print_report_neracalajur', $data);
			}else{
				$data['data']	   = $this->GLReport->GetReportDataArusKas('RPT003');
				$this->load->view('keu_setup_report_item/print_report_aruskas', $data);
			}
			
	}
	
	
	function create()
	{
		$data['title'] = "Tambah Setup Report Item";
		$data['data'] = NULL;
		$data['arr_report'] = json_encode($this->GLReport->SWReportType());
		$data['arr_coa'] = json_encode($this->GLReport->KeuCOA());
		$data['arr_item_coa'] = json_encode($this->GLReport->KeuITEMCOA());
		
		$data['mode'] = "create";
		viewByTemplate('keu_setup_report_item/form', $data, 'form');
	}

	function edit()
	{
		$data['title'] = "Ubah Setup Report Item";
		$data['data'] = $this->GLReport->GetItemSingle(get('id_item'));
				
		$data['arr_report'] = json_encode($this->GLReport->SWReportType());
		$data['arr_coa'] = json_encode($this->GLReport->KeuCOA(get('id_item')));
		$data['arr_item_coa'] = json_encode($this->GLReport->KeuITEMCOA(get('id_item')));
		
		$data['mode'] = "edit";
		viewByTemplate('keu_setup_report_item/form', $data, 'form');
	}
	
	
	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");

		
		// Validation
		
		if (post("no_urut") == '') {
			$error->Add("no_urut", "No. Urut", $tpl_empty);
		}
		
		if (post("nama_item") == '') {
			$error->Add("nama_item", "Nama Item", $tpl_empty);
		}
		
		
		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
				$data	= post_by_key(array('kd_report', 'id_item', 'id_item_induk', 'no_urut', 'nama_item', 'status_penomoran'));
				
				
				$data['id_item']			= toNumber($data['id_item']);
				
				if ($data['id_item_induk']=='') 
					$data['id_item_induk'] = null;
				else
					$data['id_item_induk']	= toNumber($data['id_item_induk']);
				
				$data['no_urut']	= toNumber($data['no_urut']);
				
				//if ($data['id_item_induk'] == '') unset($data['id_item_induk']);
				
				if (post("mode") == "create")
				{
					$id_item		= $this->get_data("SELECT (NVL((SELECT MAX(id_item) FROM keu_setup_report_item),0)+1) code FROM DUAL");
					
					$data['id_item']					= $id_item;
					$data['create_date']		= NOW;
					$data['create_user']		= USERID;

					$this->GLReport->AddItem($data);

					$ls_response = "Data Tersimpan";
					//App.audit_trail("CS001", "create/" + (data.role_id ?? ""));

				}
				else if (post("mode") == "edit")
				{
				
					$data['modify_date']	= NOW;
					$data['modify_user']	= USERID;
					
					$this->GLReport->UpdateItem($data, 
										array('id_item'	=> $data['id_item']));

					$ls_response = "Data Tersimpan";
					//App.audit_trail("CS002", "edit/" + (data.role_id ?? ""));

				}
				
				
				// Detail Payment
				$dtl_account	= $this->data['dtl_account'];
				$total_inserted	= $dtl_account['num_row']['inserted'];
				
				$this->GLReport->DeleteItemCoa($data['id_item']);
				
				for($i=0,$max_i=$total_inserted; $i<$max_i; $i++){
					$row = $dtl_account['inserted'][$i];
					
					unset($row['nama_coa']);
					$row['id_item'] = $data['id_item'];
					$row['create_date']	= NOW;
					$row['create_user']	= USERID;
					$this->GLReport->AddCoa($row);
				}
				
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result	= "";
		$id_item	= get('id_item');
		
		if ($id_item != "")
		{
			// Delete Single
			$ls_result = $this->GLReport->DeleteItemSingle($id_item);

		}
		else
		{
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				$ls_result .= $this->GLReport->DeleteItemSingle($row['id_item`']);
			}
		}
		
		echo $ls_result;
	}
}
?>